package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.project.system.domain.CatServiceReply;
import com.happy.cat.project.system.service.ICatServiceReplyService;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 自动回复Controller
 *
 * @author verne
 * @date 2020-08-07
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/catServiceReply" )
@Api("自动回复 API")
public class CatServiceReplyController extends BaseController {

    private final ICatServiceReplyService iCatServiceReplyService;

    /**
     * 查询自动回复列表
     */
    @ApiOperation(value = "查询自动回复列表",notes = "查询自动回复列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:catServiceReply:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatServiceReply catServiceReply)
    {
        startPage();
        LambdaQueryWrapper<CatServiceReply> lqw = new LambdaQueryWrapper<CatServiceReply>();
        if (StringUtils.isNotBlank(catServiceReply.getKeyWord())){
            lqw.eq(CatServiceReply::getKeyWord ,catServiceReply.getKeyWord());
        }
        if (StringUtils.isNotBlank(catServiceReply.getReplyWord())){
            lqw.eq(CatServiceReply::getReplyWord ,catServiceReply.getReplyWord());
        }
        if (catServiceReply.getEnters() != null){
            lqw.eq(CatServiceReply::getEnters ,catServiceReply.getEnters());
        }
        if (catServiceReply.getCreateDate() != null){
            lqw.eq(CatServiceReply::getCreateDate ,catServiceReply.getCreateDate());
        }
        if (catServiceReply.getStatus() != null){
            lqw.eq(CatServiceReply::getStatus ,catServiceReply.getStatus());
        }
        List<CatServiceReply> list = iCatServiceReplyService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出自动回复列表
     */
    @ApiOperation(value = "导出自动回复列表",notes = "导出自动回复列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catServiceReply:export')" )
    @Log(title = "自动回复" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatServiceReply catServiceReply) {
        LambdaQueryWrapper<CatServiceReply> lqw = new LambdaQueryWrapper<CatServiceReply>(catServiceReply);
        List<CatServiceReply> list = iCatServiceReplyService.list(lqw);
        ExcelUtil<CatServiceReply> util = new ExcelUtil<CatServiceReply>(CatServiceReply. class);
        return util.exportExcel(list, "catServiceReply" );
    }

    /**
     * 获取自动回复详细信息
     */
    @ApiOperation(value = "获取自动回复详细信息",notes = "获取自动回复详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catServiceReply:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatServiceReplyService.getById(id));
    }

    /**
     * 新增自动回复
     */
    @ApiOperation(value = "新增自动回复",notes = "新增自动回复",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catServiceReply:add')" )
    @Log(title = "自动回复" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatServiceReply catServiceReply) {
        catServiceReply.setCreateDate(new Date());
        catServiceReply.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatServiceReplyService.save(catServiceReply) ? 1 : 0);
    }

    /**
     * 修改自动回复
     */
    @ApiOperation(value = "修改自动回复",notes = "修改自动回复",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catServiceReply:edit')" )
    @Log(title = "自动回复" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatServiceReply catServiceReply) {
        return toAjax(iCatServiceReplyService.updateById(catServiceReply) ? 1 : 0);
    }

    /**
     * 删除自动回复
     */
    @ApiOperation(value = "删除自动回复",notes = "删除自动回复",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:catServiceReply:remove')" )
    @Log(title = "自动回复" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatServiceReplyService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
